Imports Microsoft.VisualBasic
Imports System.Data.Sql
Imports System.Data.SqlClient
Imports System
Imports System.Data

Imports GTSDALTableAdapters
Imports GTSDAL
Imports GTSSystem.BLL.IdGenerator
Imports GTSSystem.BLL.Location
Imports GTSSystem.BLL.ContactInfo

Namespace GTSSystem.BLL
    <System.ComponentModel.DataObject()> _
    Public Class Client

        Private _clientAdaptor As ClientTableAdapter = Nothing
        Protected ReadOnly Property ClientAdaptor() As ClientTableAdapter
            Get
                If _clientAdaptor Is Nothing Then
                    _clientAdaptor = New ClientTableAdapter
                End If
                Return _clientAdaptor
            End Get
        End Property
        Public Sub New()
            'Constructor Code here
        End Sub
        <System.ComponentModel.DataObjectMethod(ComponentModel.DataObjectMethodType.Insert, True)> _
               Public Function CreateClientBei(ByVal ClientName As String, _
                                               ByVal ContractStartDate As Date, _
                                               ByVal ContractEndDate As Date, _
                                               ByVal ContactName As String, _
                                               ByVal Phone As String, _
                                               ByVal Fax As String, _
                                               ByVal Email As String, _
                                               ByVal StreetAddress As String, _
                                               ByVal City As String, _
                                               ByVal ProvinceState As String, _
                                               ByVal Country As String, _
                                               ByVal PostalCode As String) As Integer
            Dim ContactMgr As New ContactInfo
            Dim LocationMgr As New Location
            Dim LocationPK As Integer
            Dim ContactInfoPK As Integer
            Dim ClientPK As Integer
            Dim StatusId As Integer
            Dim Result As Integer
            StatusId = 1
            Dim BranchMgr As New GTSSystem.BLL.ClientBranch

            Try
                'Add location > get location Id
                LocationPK = LocationMgr.addLocation(City, ProvinceState, Country, PostalCode)

                'Add ContactInfo > get Contactininfo id
                ContactInfoPK = ContactMgr.addContactInfo(LocationPK, ContactName, Phone, Fax, Email, StreetAddress)

                'Add Client > get clientid
                ClientPK = GetNextId("Client")
                If (ClientPK > 0) Then
                    Result = ClientAdaptor.Insert(ClientPK, ContactInfoPK, ClientName, StatusId, ContractStartDate, ContractEndDate)
BranchMgr.CreateClientBranch(ClientPK,1,ClientName,
                    CreateClientBei = Result
                Else
                    Throw New Exception("Client PK: Not generated")
                End If

            Catch ex As Exception
                Throw
            End Try
        End Function

        <System.ComponentModel.DataObjectMethod(ComponentModel.DataObjectMethodType.Insert, True)> _
        Public Function CreateClient(ByVal ClientName As String, _
                                        ByVal StatusId As Integer, _
                                        ByVal ContractStartDate As Date, _
                                        ByVal ContractEndDate As Date, _
                                        ByVal ContactName As String, _
                                        ByVal Phone As String, _
                                        ByVal Fax As String, _
                                        ByVal Email As String, _
                                        ByVal StreetAddress As String, _
                                        ByVal City As String, _
                                        ByVal ProvinceState As String, _
                                        ByVal Country As String, _
                                        ByVal PostalCode As String) _
                                      As Integer
            Dim ContactMgr As New ContactInfo
            Dim LocationMgr As New Location
            Dim LocationPK As Integer
            Dim ContactInfoPK As Integer
            Dim ClientPK As Integer
            Dim ClientRateLocationAdapter As New RateLocationTableAdapter

            Try
                'Add location > get location Id
                LocationPK = LocationMgr.addLocation(City, ProvinceState, Country, PostalCode)

                'Add ContactInfo > get Contactininfo id
                ContactInfoPK = ContactMgr.addContactInfo(LocationPK, ContactName, Phone, Fax, Email, StreetAddress)

                'Add Client > get clientid
                ClientPK = GetNextId("Client")
                If (ClientPK > 0) Then
                    CreateClient = ClientAdaptor.Insert(ClientPK, ContactInfoPK, ClientName, StatusId, ContractStartDate, ContractEndDate)
                Else
                    Throw New Exception("Client PK: Not generated")
                End If

            Catch ex As Exception
                Throw
            End Try
        End Function

        <System.ComponentModel.DataObjectMethod(ComponentModel.DataObjectMethodType.Update, True)> _
        Public Function UpdateClient(ByVal ClientId As Integer, _
                                        ByVal ContactInfoId As Integer, _
                                        ByVal LocationId As Integer, _
                                        ByVal ClientName As String, _
                                        ByVal StatusId As Integer, _
                                        ByVal ContractStartDate As Date, _
                                        ByVal ContractEndDate As Date, _
                                        ByVal ContactName As String, _
                                        ByVal Phone As String, _
                                        ByVal Fax As String, _
                                        ByVal Email As String, _
                                        ByVal StreetAddress As String, _
                                        ByVal City As String, _
                                        ByVal ProvinceState As String, _
                                        ByVal Country As String, _
                                        ByVal PostalCode As String) _
                                      As Integer

            Dim ContactMgr As New ContactInfo
            Dim LocationMgr As New Location
            Try
                'Update location 
                LocationMgr.updateLocation(LocationId, City, ProvinceState, Country, PostalCode)

                'Update ContactInfo 
                ContactMgr.updateContactInfo(ContactInfoId, LocationId, ContactName, Phone, Fax, Email, StreetAddress)

                'Update Client 
                ClientAdaptor.Update(ContactInfoId, ClientName, StatusId, ContractStartDate, ContractEndDate, ClientId)

            Catch ex As Exception
                Throw
            End Try
        End Function

        Public Shared Function LookupClientbyClientId(ByVal ClientId As Integer) As ClientINFODataTable
            Dim ClientAdapter As New GTSDALTableAdapters.ClientINFOTableAdapter
            Try
                LookupClientbyClientId = ClientAdapter.GetClientINFOByClientID(ClientId)
            Catch ex As Exception
                Throw
            End Try
        End Function
        Public Shared Function getAllClients() As ClientDataTable
            Dim ClientAdapter As New ClientTableAdapter
            Try
                getAllClients = ClientAdapter.GetClient()
            Catch ex As Exception
                Throw
            End Try
        End Function
        'TODO:
        'Public Function DeleteClient(ByVal ClientId As Integer) As Integer
        '    Membership.GetUser("test").
        '    Dim ClientInfoId As Integer
        '    Dim LocationId As Integer
        '    Dim ClientData As New ClientDataTable
        '    Try
        '        ClientData = ClientAdaptor.GetClientByClientID(ClientId)
        '        ClientInfoId = 

        '    Catch ex As Exception
        '        Throw
        '    End Try
        'End Function


        Public Shared Function LookupClientbyClientName(ByVal ClientName As String) As ClientDataTable
            Dim ClientAdapter As New ClientTableAdapter
            Try
                LookupClientbyClientName = ClientAdapter.GetClientByClientName(ClientName)
            Catch ex As Exception
                Throw
            End Try
        End Function


    End Class
End Namespace


